package com.ltmall.api.dao.impl;

import com.ltmall.api.bean.AreaPostage;
import com.ltmall.api.bean.LogisticPattern;
import com.ltmall.api.bean.LogisticPatternExc;
import com.ltmall.api.bean.OrderLogistic;
import com.ltmall.api.dao.ILogisticDAO;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import java.sql.SQLException;
import java.util.List;

public class LogisticDAO extends BaseEntityDAO<LogisticPattern> implements ILogisticDAO {

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List<LogisticPatternExc> getPatternExc(final int patternID) {
		// TODO Auto-generated method stub
		final String HQL = "from LogisticPatternExc patExc where patExc.patternID = ?";
		List<LogisticPatternExc> list = this.getHibernateTemplate().execute(
				new HibernateCallback<List>() {
					public List doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query query = session.createQuery(HQL);
						query.setParameter(0, patternID);
						return query.list();
					}
				});
		return list;
	}

	@SuppressWarnings("rawtypes")
	public LogisticPattern getPattern(int patternID) {
		List list = this.getHibernateTemplate().find(
				"from LogisticPattern pat where pat.patternID =?",
				new Object[] { patternID });
		if (list.size() > 0) {
			return (LogisticPattern) list.get(0);
		}
		return null;
	}

	public boolean addOrderLogistic(OrderLogistic logistic) {
		logistic.setOpTime(System.currentTimeMillis());// 设置时间
		try {
			getHibernateTemplate().persist(logistic);
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	@SuppressWarnings("rawtypes")
	public LogisticPatternExc getPatternExc(int patternID, String address, int type) {
		
		String hql ;
		switch(type){
		case 1:
			hql = "from LogisticPatternExc exc where exc.patternID =? and exc.city like '%"+address+"%'";
			break;
		case 2:
			hql = "from LogisticPatternExc exc where exc.patternID =? and exc.province like '%"+address+"%'";
			break;
		default :
			hql = "from LogisticPatternExc exc where exc.patternID =? and exc.zone like '%"+address+"%'";	
		}
		 
		List list = this.getHibernateTemplate().find(hql,new Object[] { patternID });
		if (list.size() > 0) {
			return (LogisticPatternExc) list.get(0);
		}
		return null;
	}

	public boolean setOrderID(int orderID, int logisticID) {
		// TODO Auto-generated method stub
		String hql = "update OrderLogistic oLogi set oLogi.orderID = ?"
				+ "where oLogi.ID = ?";
		try {
			Session session = getSession();
			Query query = session.createQuery(hql);
			query.setParameter(0, orderID);
			query.setParameter(1, logisticID);
			int temp = query.executeUpdate();
			if (temp == 1) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@SuppressWarnings("rawtypes")
	public LogisticPatternExc getPatternExc(int patternID, String province,
			String city) {
		List list = this.getHibernateTemplate().find(
				"from LogisticPatternExc pate where pate.patternID =? and pate.city = ?",
				new Object[] { patternID,city});
		if (list.size() > 0) {
			return (LogisticPatternExc) list.get(0);
		}
		return null;
	}
	
	@SuppressWarnings("rawtypes")
	public LogisticPatternExc getPatternExcNoCity(int patternID, String province) {
		List list = this.getHibernateTemplate().find(
				"from LogisticPatternExc pate where pate.patternID =? and pate.province = ?",
				new Object[] { patternID ,province});
		if (list.size() > 0) {
			return (LogisticPatternExc) list.get(0);
		}
		return null;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public int logiType(final int goodsID) {
		// TODO Auto-generated method stub
		final String SQL = "SELECT FLP.TYPE " +
				"FROM FLAVOR_LOGISTIC_PATTERN FLP LEFT JOIN FLAVOR_GOODS FG ON FLP.PATTERN_ID = FG.GOODS_LOGISTIC " +
				" WHERE FG.GOODS_ID = ?";
		List<String> list = this.getHibernateTemplate().execute(
				new HibernateCallback<List>() {
					public List doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query query = session.createSQLQuery(SQL);
						query.setParameter(0, goodsID);
						return query.list();
					}
				});
		if (list.size() == 0) {
			return 0;
		} else {
			Object obj = list.get(0);
			return Integer.parseInt(obj.toString());
		}
	}

	@SuppressWarnings("rawtypes")
	public AreaPostage getPostage(int patternID, String area,int type) {
		
		String hql = "";
		if(type==1){
			//省份
			hql = "from AreaPostage ap where ap.patternID =? and ap.city = ?"; 
		}else{
			//市
			hql = "from AreaPostage ap where ap.patternID =? and ap.province = ?";
		}
		List list = this.getHibernateTemplate().find(hql,new Object[] { patternID,area});
		if (list.size() > 0) {
			return (AreaPostage) list.get(0);
		}
		return null;
	}

	@SuppressWarnings("rawtypes")
	public AreaPostage getNoPostage(int patternID, String area, int type) {
		// TODO Auto-generated method stub
		String hql = "";
		Object obj[] = null ;
		if(type==1){
			//不包邮的市
			hql = "from AreaPostage ap where ap.patternID =? and ap.nation = ?  and ap.noCity =?";
			obj = new Object[] { patternID,"全国",area};
		}else {
			//不包邮的省
			hql = "from AreaPostage ap where ap.patternID =? and ap.nation = ?  and ap.noProvince =?";
			obj = new Object[] { patternID,"全国",area};
		}
		List list = this.getHibernateTemplate().find(hql,obj);
		if (list.size() > 0) {
			return (AreaPostage) list.get(0);
		}
		return null;
	}

	@SuppressWarnings("rawtypes")
	public AreaPostage getNationPostage(int patternID) {
		// TODO Auto-generated method stub
		List list = this.getHibernateTemplate().
				find("from AreaPostage ap where ap.patternID =? and ap.nation =? and ap.postage !=0",
				new Object[] { patternID,"全国"});
		if (list.size() > 0) {
			return (AreaPostage) list.get(0);
		}
		return null;
	}

}
